/*
 * TCP asynchronous implementation, that is, TCP link events are informed to client 
 * through asynchronous mode. Events informed to client involve:
 * VM_TCP_EVT_CONNECTED
 * After TCP link connected inform this event to client, and meanwhile TCP can be read and written.
 *
 * VM_TCP_EVT_CAN_READ
 * After user reading blocked, TCP link has readable data again, this event will return to client 
 * and inform client to continue reading. When client call vm_tcp_read function, return 0 to indicate
 * that the current read channel is in block status, client should not continue to call vm_tcp_read 
 * function, but wait for receiving VM_TCP_EVT_CAN_READ event and then continue reading. 
 * 
 * VM_TCP_EVT_CAN_WRITE
 * After user writing blocked, TCP link has space to write again, this event will return to client 
 * and inform client to continue wiring. When client call vm_tcp_write funtciton, return 0 to indicate 
 * that the current write channel is in block status, client should not continue to call the vm_tcp_write 
 * function, but wait for receiving VM_TCP_EVT_CAN_WRITE event and then continue writing.
 *
 * VM_TCP_EVT_PIPE_BROKEN
 * This event will asynchronously return to client when TCP channel connection fails. After receive this 
 * event, client should reset TCP connection, but not to read and write data into channel, and not to 
 * continue calling vm_tcp_clise function.
 *
 * VM_TCP_EVT_HOST_NOT_FOUND
 * This event only returns at CMNET connecting. In connecting, if acquire IP address from host fails, this 
 * event will return to client. After receive this event, client should not continue to read and write data 
 * in channel, and not call vm_tcp_close function, this TCP link will close automatically.
 *
 * VM_TCP_EVT_PIPE_CLOSED
 * Set TCP link, that is, client program receives VM_TCP_EVT_CONNECTED; in this case, if any one of the two 
 * nodes in communication close TCP connection, client will receive this event, even if client calls 
 * vm_tcp_close function by self, it still is informed of this event. 
 *
 */

#ifndef VM_SOCK_H
#define VM_SOCK_H

#ifdef __cplusplus
extern "C" {
#endif

#include "vmsys.h"

/**
 * TCP connection is created, and it is readable/writeable
 */
#define VM_TCP_EVT_CONNECTED	1

/**
 * TCP connection is writeable
 */
#define VM_TCP_EVT_CAN_WRITE	2

/**
 * TCP connection is readable
 */
#define VM_TCP_EVT_CAN_READ		3

/**
 * TCP connection is broken
 */
#define VM_TCP_EVT_PIPE_BROKEN	4

/**
 * Host is not found. This is return status after DNS failure.
 */
#define VM_TCP_EVT_HOST_NOT_FOUND	5

/*
 * Event occurred after TCP connection is closed
 */
#define VM_TCP_EVT_PIPE_CLOSED	6

/**
 * create TCP connection failed because of no enough resource
 */
#define VM_TCP_ERR_NO_ENOUGH_RES	-1

/**
 * create TCP connection failed
 */
#define VM_TCP_ERR_CREATE_FAILED	-2

/**
 * create TCP connection using GPRS/CMNET account
 */
#define VM_TCP_APN_CMNET	1

/**
 * create TCP connection using GPRS/CMWAP account
 */
#define VM_TCP_APN_CMWAP	2

/**
 * create TCP connection. 
 * This is an asynchronous method, which connection status is transmitted to application 
 * through callback function. If system returns TCP connection handles which is greater 
 * than or equal to 0, connection will be able t read and write. In VRE network read and 
 * write are all in asynchronous mode, which is decided on VRE single tasking feature, 
 * application must read and write network data with this asynchronous mode.
 *
 * @param host				Remote host can be an IP address of dotted-decimal format
 * @param port				Remote host port
 * @param apn					GPRS account used in connection must be VM_TCP_APN_CMNET or VM_TCP_APN_CMWAP. 
 *										CMNET is an APN provided by China Mobile for direct connect to internet, in 
 *										this case that mobile phone can be regarded as a MODEM. CMWAP is an APN only 
 *										being used in WAP service, and application connects to network through the 
 *										proxy server of China Mobile WAP GATEWAY, but DNS is off in default, so only 
 *										can visit 10.0.0.172 proxy server.
 * @param callback		callback function. It is used for system to transmit TCP link status to 
 *										application. 
 *
 * @return						0					- succeed
 *										negative	- fail
 */
VMINT vm_tcp_connect(const char* host, const VMINT port, const VMINT apn,
	void (*callback)(VMINT handle, VMINT event));

/**
 * close TCP connection
 *
 * @param handle			connection handle returned by vm_tcp_connect()
 */
void vm_tcp_close(VMINT handle);

/**
* end of TCP connection data steam
*/
#define VM_TCP_READ_EOF				(-1)

/**
 * read specified number of bytes from TCP connextion
 *
 * @param handle			TCP connection handle
 * @param buf					pointer to the buffer that recieves the data from TCP connection
 * @param len					length of buffer
 *
 * @return						the number of bytes read. if there is no data in the TCP connection,
 *										the function retruns 0, and the connection is blocked. The value of VM_TCP_READ_EOF
 *										indicates the end of stream. At that time, connection must be closed by calling 
 *										vm_tcp_close to close the connection. Other negative return values represent errors 
 *										in the connection.
 */
VMINT vm_tcp_read(VMINT handle, void* buf, VMINT len);

/**
 * write specified bytes into the connection
 *
 * @param handle		TCP connection handle
 * @param buf				pointer to the buffer that stores the data to be written into the TCP connection
 * @param len				the number of bytes to be written
 *
 * @return					the number of bytes written
 */
VMINT vm_tcp_write(VMINT handle, void* buf, VMINT len);


/**
 * Is WI-F Isupported in.current phone environment 
 *
 * @return 		Is Wi-Fi supported.
 * @retval 		FALSE: Not supported.
 * @retval 		TRUE: Supported.
 */
VMINT vm_is_support_wifi(void);

#ifdef __cplusplus
}
#endif

#endif